/*
 * ChatSDApp.java
 */
package chatserver;

import com.sd.chat.ChatServerView;
import com.sd.chat.ServerMain;
import java.rmi.RMISecurityManager;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class ChatSDApp extends SingleFrameApplication {

    private ChatSDView serverView;
    private ServerMain chatServer;
    private boolean iAmMain;
    private String serverip = "127.0.0.1";
    private String backupserverip = "127.0.0.1";

    /**
     * At startup create and show the main frame of the application.
     */
    @Override
    protected void startup() {
        serverView = new ChatSDView(this);
        chatServer = new ServerMain("Chat Server", (ChatServerView) serverView, serverip, backupserverip, iAmMain);
        serverView.setServerLink(chatServer);
        chatServer.start();
        show(serverView);

    }

    @Override
    protected void initialize(String[] args) {

        if (args[0].equals("main")) {
            this.iAmMain = true;
        } else {
            this.iAmMain = false;
        }
        if(args.length>1){
            serverip = args[1];
        }
        if(args.length>2){
            backupserverip = args[2];
        }
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override
    protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of ChatSDApp
     */
    public static ChatSDApp getApplication() {
        return Application.getInstance(ChatSDApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        System.getProperties().put("java.security.policy", "security.policy");
        System.setSecurityManager(new RMISecurityManager());
        launch(ChatSDApp.class, args);
    }
}
